<?php
/**
 * sfValidatorInvoice Valida un numero de factura
 *
 * @author     Seewald Walter <walterseewald@gmail.com>
 * @version    sfValidatorInvoice.class.php 1.0 2010-08-25 00:43:00 $
 * @filesource
 * @license GPL
 */
 
class sfValidatorInvoice extends sfValidatorBase {
 
    protected function configure($options = array(), $messages = array()) 
    {	
		$this->setMessage('invalid', '"%invoice%" no es un numero de factura valido.');
		$this->addMessage('mustInt', 'El numero de factura debe ser numerico.');
		$this->addMessage('noZero', 'El numero de factura debe ser mayor a cero.');
    }
 
    protected function doClean($value)
    {
		//supongo q siempre viene un array de 2 elementos
		if(!is_numeric($value['talonario']) || !is_numeric($value['factura'])){
			throw new sfValidatorError($this, 'mustInt', array());
		}
		if(intval($value['talonario'])<1 || intval($value['factura'])<1){
			throw new sfValidatorError($this, 'noZero', array());
		}
			
		$t = '0000'.$value['talonario'];
		$f = '00000000'.$value['factura'];
		$talonario = substr($t,(strlen($t)-4),4);
		$factura = substr($f,(strlen($f)-8),8);

		return $talonario.'-'.$factura;
    }
}
?>